//
//  CouponViewController.m
//  HeadYang
//
//  Created by MingmingSun on 15/8/22.
//  Copyright (c) 2015年 xiaoluoli. All rights reserved.
//

#import "CouponViewController.h"

@interface CouponViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) NSMutableArray *barberArray;
@property(nonatomic,weak) IBOutlet UITableView *listView;

@end

@implementation CouponViewController

@synthesize listView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"可用优惠券";
        self.navigationItem.leftBarButtonItem =
        [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_white"]
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(backPressed:)];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"不用优惠券" style:UIBarButtonItemStyleDone target:self action:@selector(nonePressed:)];
        self.barberArray = [NSMutableArray array];
    }
    return self;
}

-(void)nonePressed:(id)sender{
    APPALL.couponDic = nil;
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)backPressed:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSDictionary *para =
    @{@"used":@"no",
      @"expired":@"no",
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"coupon_list" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                    break;
                case 1:
                {
                    NSLog(@"yeah1....");
                    [_barberArray removeAllObjects];
                    NSArray *cityArray = [aSucData objectForKey:@"coupon"];
                    for (NSDictionary *dic in cityArray) {
                        [_barberArray addObject:dic];
                    }
                    [listView reloadData];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.barberArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.0f;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *inde;
    inde= @"CouponID";
    cell = [tableView dequeueReusableCellWithIdentifier:inde];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CouponCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
    UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
    UILabel *lab3 = (UILabel*)[cell viewWithTag:3];
    NSDictionary *cacheItem = [_barberArray objectAtIndex:indexPath.row];
    lab1.text = [NSString stringWithFormat:@"%.2f",[[cacheItem valueForKey:@"price"] floatValue]];
    float price = [[cacheItem valueForKey:@"minCost"] floatValue];
    if(price <= 0)
        lab2.text = @"无限制使用";
    else
        lab2.text = [NSString stringWithFormat:@"满%.2f元可用",price];
    lab3.text = [NSString stringWithFormat:@"有效期至:%@",[cacheItem valueForKey:@"scrapTime"]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    APPALL.couponDic = [_barberArray objectAtIndex:indexPath.row];
    [self useCoupon];
}

-(void)useCoupon{
    NSDictionary *para =
    @{@"id":[APPALL.orderDetailDic valueForKey:@"id"],
      @"coupon":[APPALL.couponDic valueForKey:@"couponNum"],
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"order_useCoupon" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                    break;
                case 1:
                {
                    NSLog(@"yeah1....");
//                    APPALL.orderDetailDic = [aSucData objectForKey:@"order"];
                    [self.navigationController popViewControllerAnimated:YES];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

@end